#include "transform.h"

Transform::Transform(d_Mat4f &m, Object3D *o)
{
	tr = inv(m);
	//m.print();
	//cout << endl;
	object = o;
}

bool Transform::intersect(const Ray &r, Hit &h, float tmin)
{
	d_Vec4f transvec = tr*d_Vec4f(r.getOrigin(),1);
	d_Vec4f transdir = tr*d_Vec4f(r.getDirection(),0);
	Hit tmphit(h.getT()*len(transdir), h.getMaterial(), h.getNormal());

	if(object->intersect(Ray(d_Vec3f(transvec[0],transvec[1],transvec[2]), norm(d_Vec3f(transdir[0],transdir[1],transdir[2]))),tmphit,tmin))
	{
		d_Vec4f normal = trans(tr)*d_Vec4f(tmphit.getNormal(), 0);
		h.set(tmphit.getT()/len(transdir), tmphit.getMaterial(), norm(d_Vec3f(normal[0], normal[1], normal[2])));
		return true;
	}
	return false;
}

bool Transform::inside(d_Vec3f point)
{
	d_Vec4f transvec = tr*d_Vec4f(point,1);
	return object->inside(d_Vec3f(transvec[0],transvec[1],transvec[2]));
}

